package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Sensor;
import model.SensorController;

public class graphLabel extends JPanel{

	// Establish variables for this program
	int Xleft = 50;
	int Xright = 250;
	int Ytop = 400; // The y-value entries can be up to 250-100=150.
	int Ybottom = 500;

	int teller;
	int num;
	int numberOfScores;
	int totalX, totalY;
	double[] scores;
	char graphChoice;
	private Sensor workingSensor;

	
	public graphLabel(Sensor s) {
		  workingSensor = s;
		  numberOfScores = workingSensor.getBuffer().maxSize();
		  
		  
	      scores = new double[numberOfScores];
	      for (int i = 0; i < scores.length;  i++)
	                scores[ i ] = 0;
	      totalX = Xright - Xleft + 1;
	      totalY = Ybottom - Ytop + 1;
	      graphChoice = 'L';
	      	      
	      setSize(400, 600);
	      setVisible(true);
	      
	}

	@Override
	protected void paintComponent(Graphics g) {
		getInputData();
		g.setColor(Color.red); // set color of the graph
		drawLineGraph(g);
		g.drawString("Past 20 values of "+workingSensor.getName(), 100, 290); // title
		g.drawString(" Scale: x10", 100, 300);
	}

	// Get input from user
	public void getInputData() {
		Object[] values = workingSensor.getBuffer().toArray();
		for (int i = 0; i < values.length; i++) {
			Double resultDouble = (Double) values[i];
			scores[i] = resultDouble.intValue();
			scores[i] = scores[i] / 10;
		}
	}

	// Draw the line graph
	public void drawLineGraph(Graphics g) {
		drawAxes(g);
		int i, x1, y1, x2, y2, largestNumber, xIncrement, yIncrement;
		// Compute the x and y increments
		largestNumber = (int) findLargest(scores);
		xIncrement = totalX / numberOfScores;
		if (largestNumber == 0)
			yIncrement = 0;
		else
			yIncrement = totalY / largestNumber;

		// Set the initial origin point
		x1 = Xleft;
		y1 = Ybottom;

		// Compute and plot the data points
		for (i = 0; i < numberOfScores; i++) {
			x2 = (int) getXCoordinate(i + 1, xIncrement);
			y2 = (int) getYCoordinate(scores[i], yIncrement);
			g.fillOval(x2, y2, 5, 5);
			g.drawLine(x1, y1, x2, y2);
			x1 = x2;
			y1 = y2;
		}

		// Label y - axes with quantity of each grade
		int topy;
		if (largestNumber % 10 == 0)
			topy = largestNumber;
		else
			topy = (largestNumber / 10 + 1) * 10;

		// i = i+5 controls y value label -- adjust for size of data
		for (i = 0; i <= topy; i = i + 1) {
			g.drawString(String.valueOf(i), 30, Ybottom - i * yIncrement + 1);
		}
		
	}

	// Draw the axes for the graph
	public void drawAxes(Graphics g) {
		g.drawLine(Xleft, Ytop, Xleft, Ybottom);
		g.drawLine(Xleft, Ybottom, Xright, Ybottom);
		
	}

	// Determining x coordinate
	public double getXCoordinate(double i, double xIncrement) {
		return Xleft + xIncrement * i;
	}

	// Determining y coordinate
	public double getYCoordinate(double num, double yIncrement) {
		return Ybottom - yIncrement * num;
	}

	// Finding the largest value in the array
	public double findLargest(double[] a) {
		int location = 0;
		for (int i = 1; i < a.length; i++)
			if (a[i] > a[location])
				location = i;
		return a[location];
	}

}
